package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.Menu;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.MenuQueryObj;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IMenuService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @RequestMapping("")
    @RequiresPermissions("employee:index")
    @PermissionName("菜单列表")
    public String index() {
        //只负责跳转到menu.jsp页面即可
        return "menu";
    }


    //查询所有的菜单
    @RequestMapping("listMenus")
    @ResponseBody
    public List<Menu> listMenus() {

        List<Menu> list = menuService.listMenus();
        return list;
    }

    @RequestMapping("listIdsByRoleId")
    @ResponseBody
    public List<Integer> listIdsByRoleId(Long roleId) {

        List<Integer> ids = menuService.listIdsByRoleId(roleId);
        return ids;
    }

    //-------------------------------------------------------------------------
    //查询所有目录菜单
    @RequestMapping("selectAllParent")
    @ResponseBody
    public List<Menu> selectAll() {
        List<Menu> list = menuService.selectAllParent();
        return list;
    }

    @RequestMapping("selectChildByParentId")
    @ResponseBody
    public List<Menu> selectChildByParentId(Long parentId) {
        List<Menu> list = menuService.selectChildByParentId(parentId);
        return list;
    }

    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Menu menu) {
        JsonResult result = new JsonResult();
        try {

            menuService.insert(menu);
            result.setMessage("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("保存失败");
        }
        return result;
    }

}